package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysHangye;
import com.ruoyi.system.service.ISysHangyeService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 行业管理Controller
 *
 * @author shuyan
 * @date 2020-12-30
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/hangye")
public class SysHangyeController extends BaseController {

    private final ISysHangyeService iSysHangyeService;

    /**
     * 查询行业管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysHangye sysHangye) {
        startPage();
        List<SysHangye> list = iSysHangyeService.queryList(sysHangye);
        return getDataTable(list);
    }

    /**
     * 从缓存中获取全部
     */
    @GetMapping("/listAllWithCache")
    public AjaxResult listAllWithCache() {
        List<SysHangye> list = iSysHangyeService.listAllWithCache();
        return AjaxResult.success(list);
    }

    @GetMapping("/getByCode")
    public AjaxResult getByCode(String code) {
        SysHangye sysHangye = iSysHangyeService.getByCode(code);
        return AjaxResult.success(sysHangye);
    }

    /**
     * 根据编码查询各级行业
     */
    @RequestMapping("/getEachLevelByCode")
    public AjaxResult getEachLevelByCode(String code) {
        Map<Integer, SysHangye> eachVo = iSysHangyeService.getEachLevelByCode(code);
        return AjaxResult.success(eachVo);
    }

    /**
     * 清除缓存
     */
    @GetMapping("clearCache")
    public AjaxResult clearCache() {
        return toAjax(iSysHangyeService.clearCache() ? 1 : 0);
    }

    /**
     * 导出行业管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:hangye:export')")
    @Log(title = "行业管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysHangye sysHangye) {
        List<SysHangye> list = iSysHangyeService.queryList(sysHangye);
        ExcelUtil<SysHangye> util = new ExcelUtil<SysHangye>(SysHangye.class);
        return util.exportExcel(list, "hangye");
    }

    /**
     * 获取行业管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:hangye:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iSysHangyeService.getById(id));
    }

    /**
     * 新增行业管理
     */
    @PreAuthorize("@ss.hasPermi('system:hangye:add')")
    @Log(title = "行业管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysHangye sysHangye) {
        sysHangye.initCateByCode();
        return toAjax(iSysHangyeService.save(sysHangye) ? 1 : 0);
    }

    /**
     * 修改行业管理
     */
    @PreAuthorize("@ss.hasPermi('system:hangye:edit')")
    @Log(title = "行业管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysHangye sysHangye) {
        sysHangye.initCateByCode();
        return toAjax(iSysHangyeService.updateById(sysHangye) ? 1 : 0);
    }

    /**
     * 删除行业管理
     */
    @PreAuthorize("@ss.hasPermi('system:hangye:remove')")
    @Log(title = "行业管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iSysHangyeService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 初始化行业门类
     */
    @RequestMapping("/syncCate")
    public AjaxResult syncCate() {
        return toAjax(iSysHangyeService.syncCate());
    }
}
